package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.ActivityBean;
import cst.a931xmc.yuejian20.Bean.ActivityOrderBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ActivityModel;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.R;

public class AorderActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int aorder_id;
    private LinearLayout activity_item;
    private ImageView activity_pic;
    private TextView activity_name;
    private TextView activity_price;
    private TextView activity_intro;
    private TextView aorder_num;
    private TextView aorder_price;
    private TextView aorder_rname;
    private TextView aorder_tel;
    private TextView aorder_time;
    private TextView aorder_orderid;
    private ImageButton btn_back;
    private ActivityModel model;
    private Listener<ActivityOrderBean> listener;
    private Listener<ActivityBean> listener1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activityorder);
        init();
        initData();
        model = new ActivityModel();
        model.getOrderDetail(aorder_id,listener);
    }

    public void init(){
        activity_item = findViewById(R.id.activity_item);
        activity_pic = findViewById(R.id.activity_pic);
        activity_name = findViewById(R.id.activity_name);
        activity_price = findViewById(R.id.activity_price);
        activity_intro = findViewById(R.id.activity_intro);
        aorder_num = findViewById(R.id.aorder_num);
        aorder_price = findViewById(R.id.aorder_price);
        aorder_rname = findViewById(R.id.aorder_rname);
        aorder_tel = findViewById(R.id.aorder_tel);
        aorder_time = findViewById(R.id.aorder_time);
        aorder_orderid = findViewById(R.id.aorder_orderid);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        Intent intent = getIntent();
        aorder_id = intent.getExtras().getInt("aorder_id");
    }

    public void initData(){
        listener = new Listener<ActivityOrderBean>() {
            @Override
            public void onResponse(ActivityOrderBean l) {
                if(l.getAorder_id()!=0){
                    aorder_num.setText("x" + l.getAorder_num());
                    aorder_price.setText("￥" + l.getAorder_price());
                    aorder_rname.setText(l.getAorder_rname());
                    aorder_tel.setText(l.getAorder_tel());
                    aorder_time.setText(l.getAorder_time());
                    aorder_orderid.setText(l.getAorder_orderid());
                    model.getDetail(l.getActivity_id(),listener1);
                }
                else {
                    Toast.makeText(AorderActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFail(String message) {
                Toast.makeText(AorderActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };

        listener1 = new Listener<ActivityBean>() {
            @Override
            public void onResponse(final ActivityBean l) {
                if(l.getActivity_id()!=0){
                    activity_name.setText(l.getActivity_name());
                    activity_price.setText("￥" + l.getActivity_price());
                    activity_intro.setText(l.getActivity_intro());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getActivity_pic())
                            .placeholder(R.drawable.nopic)
                            .into(activity_pic);
                    activity_item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(AorderActivity.this,ActivityActivity.class);
                            intent.putExtra("activity_id",l.getActivity_id());
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                else {
                    Toast.makeText(AorderActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(AorderActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
        }
    }
}
